#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/28 0028 14:18
# @Author  : Arliki
# @File    : comment.py

import json, re, scrapy, time
from scrapy_redis.spiders import RedisSpider
from qqcoment.items import CommentItem


class CommentSpider(RedisSpider):
    name = "comment"
    redis_key = "qqlive:comment_url"
    custom_settings = {
        'REDIS_PARAMS': {
            'db': 3,
            'password': '5tgbnhy67ujm'
        }
    }
    def __init__(self, *args, **kwargs):
        domain = kwargs.pop("domain", "")
        self.allowed_domains = filter(None, domain.split(","))
        super(CommentSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        jsdic = re.search(".*?\((.*?)\)$", response.body.decode("utf-8")).group(1)
        jsdic = json.loads(jsdic, encoding="utf-8")
        jsdata = jsdic["data"]
        for x in jsdata["oriCommList"]:
            item = CommentItem()
            item["userid"] = x["userid"]
            item["ctime"] = x["time"]
            item["getup"] = x["up"]
            item["content"] = x["content"]
            try:
                item["usernick"] = jsdata["userList"][item["userid"]]["nick"]
                item["userhead"] = jsdata["userList"][item["userid"]]["head"]
            except:
                pass
            yield item
        # lpush qqlive:comment_url https://video.coral.qq.com/varticle/2354361441/comment/v2?callback=_varticle2354361441commentv2&orinum=30&oriorder=o&pageflag=1&cursor=0&scorecursor=0&orirepnum=2&reporder=o&reppageflag=1&source=9&_=1517135712602
